//获取应用实例
const app = getApp();
const indexData = require("../../json/index.js");
const cfg = require("../../utils/config.js");
const navigateTo = require("../../utils/navigateTo.js"); 
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    // 导航头组件所需的参数
    nvabarData: {
      showCapsule: 0, //是否显示左上角图标  1表示显示  0表示不显示
      title: '首页', //导航栏 中间的标题
      white: true, // 是就显示白的，不是就显示黑的。
      address: '../../images/home.png' // 加个背景 不加就是没有
    },
    // 导航头的高度
    height: app.globalData.height * 2 + 20,
    bannerSrc: '../../images/banner1.jpg',
    tixianIcon:'../../images/tixian_icon.png',
    chaxunIcon: '../../images/shanghu_icon.png',
    jiaoyiIcon: '../../images/mingxichaxun_icon.png',
    shouyiIcon: '../../images/shouyi_icon.png',
    xiugaiIcon: '../../images/set_icon.png',
    qiyeIcon: '../../images/qiye.png',
    gerenIcon: '../../images/geren.png',
    tianjiaIcon: '../../images/tianjia-icon@3x.png',
    fabuIcon: '../../images/fabuxiangmu-ico.png',
    chengjieIcon: '../../images/chengjiexiangmu-ico.png',
    fapiaoIcon: '../../images/dayinfapiao-ico.png',
    chaknIcon: '../../images/chakangeren-ico.png',
    chaknqiyeIcon: '../../images/chakanqiye-ico.png',
    payIcon:'../../images/pay.png',
    userIcon:'../../images/user.jpg',
    kefuIcon:'../../images/kefuIcon.png',
    dataJson:'',
    agentName:'',
    agentPerson:'',
    loginName:'',
    fullCompanyName:'',
    agentAccountBalance:'',
    payMoneyWithMonth:'',
    incomeMoneyWithMonth:'',
    authorityName: wx.getStorageSync('authorityName'),
    showModalStatus: false,
    imgUrls: [
      {
        url: '../../images/tixian_icon.png'
      }, {
        url: '../../images/shanghu_icon.png'
      }, {
        url: '../../images/mingxichaxun_icon.png'
      }
    ],
    indicatorDots: true, //小点
    indicatorColor: "white",//指示点颜色
    activeColor: "coral",//当前选中的指示点颜色
    autoplay: true, //是否自动轮播
    interval: 2000, //间隔时间
    duration: 1000, //滑动时间
    circular:true,
    extractionStatus:false, //提现按钮显示状态
    tipsImage:"../../images/timg.png",
    marqueeTextObj: {
      "Mon": ["王**,电话：158****8990,认证成功！","高**,电话：130****4212,认证成功！","肖**,电话：136****1045,认证成功！","张**,电话：131****9972,认证成功！","刘**,电话：130****8814,认证成功！","项目金额：100,000元,公司：北京友**有限公司","项目金额：20,000元,公司：北京众合**有限公司","项目金额：45,000元,公司：优**科技有限公司"],
      "Tue": ["李**,电话：138****0012,认证成功！","赵**,电话：133****4489,认证成功！","陆**,电话：139****8485,认证成功！","王**,电话：132****9043,认证成功！","康**,电话：136****1203,认证成功！","项目金额：20,000元,公司：北京华**股份有限公司","项目金额：120,000元,公司：北京天**科技有限公司","项目金额：15,000元,公司：明**科技有限公司"],
      "Wed": ["张**,电话：151****7422,认证成功！","于**,电话：130****3092,认证成功！","宫**,电话：137****1928,认证成功！","张**,电话：139****9207,认证成功！","王**,电话：132****8980,认证成功！","项目金额：120,000元,公司：北京道**科技有限公司","项目金额：25,000元,公司：风**科技有限公司","项目金额：10,000元,公司：中国亿**科技有限公司"],
      "Thu": ["钱**,电话：158****1083,认证成功！","李**,电话：130****4288,认证成功！","刘**,电话：136****1045,认证成功！","张**,电话：131****9972,认证成功！","冯**,电话：130****0214,认证成功！","项目金额：100,000元,公司：北京友**有限公司","项目金额：20,000元,公司：北京众合**有限公司","项目金额：45,000元,公司：优**科技有限公司"],
      "Fri": ["王**,电话：158****8990,认证成功！","高**,电话：130****4212,认证成功！","肖**,电话：136****1045,认证成功！","张**,电话：131****9972,认证成功！","刘**,电话：130****8814,认证成功！","项目金额：10,000元,公司：北京小**有限公司","项目金额：30,000元,公司：信**科技有限公司","项目金额：5,000元,公司：张家口**科技有限公司"],
      "Sat": ["侯**,电话：150****8273,认证成功！","周**,电话：138****9222,认证成功！","段**,电话：136****2545,认证成功！","郝**,电话：131****9092,认证成功！","姜**,电话：130****8275,认证成功！","项目金额：110,000元,公司：北京橙**有限公司","项目金额：80,000元,公司：广州美**有限公司","项目金额：15,000元,公司：蓝**科技有限公司"],
      "Sun": ["李**,电话：131****8230,认证成功！","陆**,电话：130****9273,认证成功！","焦**,电话：138****1051,认证成功！","陈**,电话：131****9076,认证成功！","张**,电话：135****3784,认证成功！","项目金额：100,000元,公司：北京盛**有限公司","项目金额：26,000元,公司：河北城**有限公司","项目金额：45,000元,公司：勒**科技有限公司"]
    },
    marqueetxtlist: [],
    mername: '', //新增变量
  },
  //事件处理函数
  bindViewTap: function() {
    navigateTo( '../logs/logs')
  },
  onLoad: function (options) {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo;
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    wx.setNavigationBarTitle({
      title: this.data.mername//页面标题为路由参数
    })
    this.getSellerId();
    this.getTxTimes();    
  },
  onShow:function(){
    wx.getSetting({
      success: (res) => {
        if (wx.getStorageSync('token') != '' && !res.authSetting['scope.userInfo']) {
          this.showModal();
        }
      }
    })
    this.setData({
      token: wx.getStorageSync('token'),
      authorityName: wx.getStorageSync('authorityName')
    })
    /*wx.showLoading({
      title: '加载中...',
    })
    */
    
    if (wx.getStorageSync('authorityName') == 'AGENT_USER'){
      this.getIndex();
    }else{
      this.setData({
        loginName: wx.getStorageSync('loginName')
      })
    }
    this.showMarqueeText();
  },
  showMarqueeText: function() {
    // 设置跑马灯的内容
    var textAry = [];
    var date = new Date()
    var today = date.getDay()
    var xDay = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
      textAry = this.data.marqueeTextObj[xDay[today]];
    this.setData({
      marqueetxtlist: textAry
    })
  },
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
      this.animation = animation;
      animation.translateY(300).step();
    this.setData({
      animationData: animation.export(),
      showModalStatus: false
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  //隐藏对话框
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
      this.animation = animation;
      animation.translateY(300).step();
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
        animation.translateY(0).step();
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
  getTxTimes:function(){ //获取提现时间并设置提现按钮是否显示；
    var _this = this;
    wx.request({
        url: cfg.requestURL + '/backend/dict/getShowTime',
      method: 'GET',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        let startTimes = res.data[0].datadicValue;
        //new Date('2020/07/10 00:00:00').getTime(); // 获取开始时间
  
        let endTimes = res.data[1].datadicValue;
        // new Date('2020/07/14 23:59:59').getTime(); //获取结束时间
        let curTimes = new Date().getDate(); //获取当前时间
        // zpj: - 设置的时间包含起始时间和结束时间，解决当月最后一天不能设置问题
        if(curTimes >= startTimes && curTimes <= endTimes){
          _this.setData({
            extractionStatus: true
          });
        }else{
          _this.setData({
              extractionStatus: false
            });
        }
      }
    })   
  },
  getSellerId:function(){
    var _this = this;
    var Url = '';
    if (wx.getStorageSync('authorityName') == 'SELLER_USER'){
      Url = '/backend/seller/mobile/getSellerByloginId';
    }
    if ((wx.getStorageSync('authorityName') == 'PERSON_USER')){
        Url = '/backend/person/mobile/getPersonByloginId';
    }
    wx.request({
        url: cfg.requestURL + Url + '?token=' + wx.getStorageSync('token') + '&loginId=' + wx.getStorageSync('userId'),
      method: 'GET',
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        wx.hideLoading();
        if (res.data.flag) {
          if (wx.getStorageSync('authorityName') == 'SELLER_USER') {
            wx.setStorageSync("sellerId", res.data.data.sellerId);
            wx.setStorageSync("sellerCompanyName", res.data.data.sellerCompanyName);
            _this.setData({
              fullCompanyName: res.data.data.sellerCompanyName
            });
          }
          if ((wx.getStorageSync('authorityName') == 'PERSON_USER')) {
            wx.setStorageSync("personId", res.data.data.personId);
          }
        } else {
          wx.showToast({
            title: res.data.msg ? res.data.msg : '',
            icon: 'none',
            mask: true,
            duration: 1500
          })
        }
      }
    })
  },
  getIndex:function(){
    var _this = this;
    wx.request({
      url: cfg.requestURL + '/backend/agent/mobile/home', //仅为示例，并非真实的接口地址
      method: 'GET',
      data: {
        agentId: wx.getStorageSync('agentId'),
        token: wx.getStorageSync('token')
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        wx.hideLoading();
        if (res.data.flag) {
          _this.setData({
            dataJson: res.data.data
          })
        } else {
          if (res.data.msg == "token错误") {
            _this.gotoLogin()
            return false;
          }
          wx.showToast({
            title: res.data.msg ? res.data.msg : '',
            icon: 'none',
            mask: true,
            duration: 1500
          })
        }
      }
    })
  },
  getUserInfo: function(e) {
    this.hideModal();
      app.globalData.userInfo = e.detail.userInfo;
      app.globalData.isok = true;
      this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  modifyPsd:function(){
    navigateTo('../modifyPsd/modifyPsd')
  },
  toEnterprise:function(){
    navigateTo('../enterpriseCertification/enterpriseCertification')
  },
  toIndividual:function () {
    if(this.data.token == ''){
      this.gotoLogin()
      return false;
    }
    navigateTo('../individualCertification/individualCertification')
  },
  toProject:function(){
    navigateTo('../postProject/postProject')
  },
  toMyProject:function(){
    navigateTo('../myProject/myProject')
  },
  toInvoicing:function(){
    if (this.data.token == '') {
      this.gotoLogin()
      return false;
    }
    navigateTo('../invoicing/invoicing')
  },
  toItem:function(){
    if (this.data.token == '') {
      this.gotoLogin()
      return false;
    }
    navigateTo('../itemList/itemList')
  },
  toPerson:function(){
    navigateTo('../viewIndividualList/viewIndividualList')
  },
  toPersonBank:function(){
    if (this.data.token == '') {
      this.gotoLogin()
      return false;
    }
    navigateTo('../addPersonBank/addPersonBank')
  },
  toBusiness:function(){
    navigateTo('../viewBusiness/viewBusiness')
  },
  toPay:function(){
    navigateTo('../payDetail/payDetail')
  },
  toBusinesses:function(){
    navigateTo('../businesses/businesses')
  },
  toLogin:function(){
    this.gotoLogin()
  },
  gotoLogin: function() {
    wx.redirectTo({
      url: '../login/login',
    })
    // navigateTo('../login/login')
  },
  quitLogin:function(){
    var _this = this;
    wx.request({
      url: cfg.requestURL + '/backend/agent/mobile/logout?token=' + wx.getStorageSync('token'), //仅为示例，并非真实的接口地址
      method: 'GET',
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.flag) {
          wx.setStorageSync("agentId", "");
          wx.setStorageSync("token", "");
          wx.setStorageSync("userId", "");
          wx.setStorageSync("authorityName", "");
          wx.setStorageSync("agentType", "");
          wx.setStorageSync("sellerId", "");
          wx.setStorageSync("personId", "");
          wx.setStorageSync("sellerCompanyName", "");
          wx.setStorageSync("loginName", "");
          wx.setStorageSync("isAuthentication", "");
          wx.setStorageSync("cardStatus", "");
          _this.gotoLogin()
          return false;
        } else {
          if (res.data.msg == "token错误"){
            _this.gotoLogin()
            return false;
          }
          wx.showToast({
            title: res.data.msg ? res.data.msg : '',
            icon: 'none',
            mask: true,
            duration: 1500
          })
        }
      }
    })
  }
})
